package com.uenpay.im.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.im.R;
import com.uenpay.im.entities.SendFile;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * 
 */
public class FolderAdapter extends BaseAdapter {

	private ArrayList<SendFile> _list = null;
	private Context _mContext;

	public FolderAdapter(ArrayList<SendFile> _listItems, Context mContext) {
		this._list = _listItems;
		_mContext = mContext;
	}

	public void setSelectedItem(int position) {
		for (int i = 0, lenght = _list.size(); i < lenght; i++) {
			_list.get(i).is_selected = 0;
		}
		_list.get(position).is_selected = 1;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return _list.size();
	}

	@Override
	public SendFile getItem(int position) {
		return _list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		SendFile file = getItem(position);
		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(_mContext).inflate(
					R.layout.file_sd_card_item, null);
			holder.nameTextView = (TextView) convertView
					.findViewById(R.id.file_name);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.selected_image);
			holder.is_file_image = (ImageView) convertView
					.findViewById(R.id.is_file_image);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.imageView.setVisibility(View.VISIBLE);
		holder.imageView.setImageResource(R.drawable.icon_uncheck);

		if (file.is_file == 0) {
			holder.is_file_image.setVisibility(View.VISIBLE);
			holder.imageView.setImageResource(R.drawable.ic_folder);

		} else {
			holder.is_file_image.setVisibility(View.GONE);
			holder.imageView.setImageResource(R.drawable.icon_uncheck);

		}

		if (file.is_selected == 1) {
			holder.imageView.setImageResource(R.drawable.icon_checked);
		}

		holder.nameTextView.setText(file.fileName);
		return convertView;
	}

	class Holder {
		private ImageView imageView, is_file_image;
		private TextView nameTextView;
	}
}
